home *** CD-ROM | disk | FTP | other *** search
/ MacAddict 104 / MacAddict_104_2005-04.iso / Software / Internet & Communication / WordPress 1.2.2 freeware.dmg / wordpress / wp-admin / admin-functions.php < prev    next >
Encoding:
PHP Script  |  2004-12-16  |  12.9 KB  |  378 lines

  1. <?php
  2.  
  3. function url_shorten ($url) {
  4.     $short_url = str_replace('http://', '', stripslashes($url));
  5.     $short_url = str_replace('www.', '', $short_url);
  6.     if ('/' == substr($short_url, -1))
  7.         $short_url = substr($short_url, 0, -1);
  8.     if (strlen($short_url) > 35)
  9.         $short_url =  substr($short_url, 0, 32).'...';
  10.     return $short_url;
  11. }
  12.  
  13. function selected($selected, $current) {
  14.     if ($selected == $current) echo ' selected="selected"';
  15. }
  16.  
  17. function checked($checked, $current) {
  18.     if ($checked == $current) echo ' checked="checked"';
  19. }
  20.  
  21. function get_nested_categories($default = 0) {
  22.  global $post_ID, $tablecategories, $tablepost2cat, $mode, $wpdb;
  23.  
  24.  if ($post_ID) {
  25.    $checked_categories = $wpdb->get_col("
  26.      SELECT category_id
  27.      FROM  $tablecategories, $tablepost2cat
  28.      WHERE $tablepost2cat.category_id = cat_ID AND $tablepost2cat.post_id = '$post_ID'
  29.      ");
  30.  } else {
  31.    $checked_categories[] = $default;
  32.  }
  33.  
  34.  $categories = $wpdb->get_results("SELECT * FROM $tablecategories ORDER BY category_parent DESC, cat_name ASC");
  35.  $result = array();
  36.  foreach($categories as $category) {
  37.    $array_category = get_object_vars($category);
  38.    $me = 0 + $category->cat_ID;
  39.    $parent = 0 + $category->category_parent;
  40.     if (isset($result[$me]))   $array_category['children'] = $result[$me];
  41.    $array_category['checked'] = in_array($category->cat_ID, $checked_categories);
  42.    $array_category['cat_name'] = stripslashes($category->cat_name);
  43.    $result[$parent][] = $array_category;
  44.  }
  45.  
  46.  return $result[0];
  47. }
  48.  
  49. function write_nested_categories($categories) {
  50.  foreach($categories as $category) {
  51.    echo '<label for="category-', $category['cat_ID'], '" class="selectit"><input value="', $category['cat_ID'],
  52.      '" type="checkbox" name="post_category[]" id="category-', $category['cat_ID'], '"',
  53.      ($category['checked'] ? ' checked="checked"' : ""), '/> ', $category['cat_name'], "</label>\n";
  54.  
  55.    if(isset($category['children'])) {
  56.      echo "\n<span class='cat-nest'>\n";
  57.      write_nested_categories($category['children']);
  58.      echo "</span>\n";
  59.    }
  60.  }
  61. }
  62.  
  63. function dropdown_categories($default = 0) {
  64.  write_nested_categories(get_nested_categories($default));
  65.  
  66. // Dandy new recursive multiple category stuff.
  67. function cat_rows($parent = 0, $level = 0, $categories = 0) {
  68.     global $wpdb, $tablecategories, $tablepost2cat, $bgcolor;
  69.     if (!$categories) {
  70.         $categories = $wpdb->get_results("SELECT * FROM $tablecategories ORDER BY cat_name");
  71.     }
  72.     if ($categories) {
  73.         foreach ($categories as $category) {
  74.             if ($category->category_parent == $parent) {
  75.                 $count = $wpdb->get_var("SELECT COUNT(post_id) FROM $tablepost2cat WHERE category_id = $category->cat_ID");
  76.                 $pad = str_repeat('— ', $level);
  77.  
  78.                 $bgcolor = ('#eee' == $bgcolor) ? 'none' : '#eee';
  79.                 echo "<tr style='background-color: $bgcolor'><th scope='row'>$category->cat_ID</th><td>$pad $category->cat_name</td>
  80.                 <td>$category->category_description</td>
  81.                 <td>$count</td>
  82.                 <td><a href='categories.php?action=edit&cat_ID=$category->cat_ID' class='edit'>" . __('Edit') . "</a></td><td><a href='categories.php?action=Delete&cat_ID=$category->cat_ID' onclick=\"return confirm('".  sprintf(__("You are about to delete the category \'%s\'.  All of its posts will go to the default category.\\n  \'OK\' to delete, \'Cancel\' to stop."), addslashes($category->cat_name)) . "')\" class='delete'>" .  __('Delete') . "</a></td>
  83.                 </tr>";
  84.                 cat_rows($category->cat_ID, $level + 1);
  85.             }
  86.         }
  87.     } else {
  88.         return false;
  89.     }
  90. }
  91.  
  92. function wp_dropdown_cats($currentcat = 0, $currentparent = 0, $parent = 0, $level = 0, $categories = 0) {
  93.     global $wpdb, $tablecategories, $tablepost2cat, $bgcolor;
  94.     if (!$categories) {
  95.         $categories = $wpdb->get_results("SELECT * FROM $tablecategories ORDER BY cat_name");
  96.     }
  97.     if ($categories) {
  98.         foreach ($categories as $category) { if ($currentcat != $category->cat_ID && $parent == $category->category_parent) {
  99.             $count = $wpdb->get_var("SELECT COUNT(post_id) FROM $tablepost2cat WHERE category_id = $category->cat_ID");
  100.             $pad = str_repeat('– ', $level);
  101.             echo "\n\t<option value='$category->cat_ID'";
  102.             if ($currentparent == $category->cat_ID)
  103.                 echo " selected='selected'";
  104.             echo ">$pad$category->cat_name</option>";
  105.             wp_dropdown_cats($currentcat, $currentparent, $category->cat_ID, $level + 1, $categories);
  106.         } }
  107.     } else {
  108.         return false;
  109.     }
  110. }
  111.  
  112. function wp_create_thumbnail($file, $max_side, $effect = '') {
  113.  
  114.     // 1 = GIF, 2 = JPEG, 3 = PNG
  115.  
  116.     if(file_exists($file)) {
  117.         $type = getimagesize($file);
  118.         
  119.         // if the associated function doesn't exist - then it's not
  120.         // handle. duh. i hope.
  121.         
  122.         if(!function_exists('imagegif') && $type[2] == 1) {
  123.             $error = __('Filetype not supported. Thumbnail not created.');
  124.         }elseif(!function_exists('imagejpeg') && $type[2] == 2) {
  125.             $error = __('Filetype not supported. Thumbnail not created.');
  126.         }elseif(!function_exists('imagepng') && $type[2] == 3) {
  127.             $error = __('Filetype not supported. Thumbnail not created.');
  128.         } else {
  129.         
  130.             // create the initial copy from the original file
  131.             if($type[2] == 1) {
  132.                 $image = imagecreatefromgif($file);
  133.             } elseif($type[2] == 2) {
  134.                 $image = imagecreatefromjpeg($file);
  135.             } elseif($type[2] == 3) {
  136.                 $image = imagecreatefrompng($file);
  137.             }
  138.             
  139.             if (function_exists('imageantialias'))
  140.                 imageantialias($image, TRUE);
  141.             
  142.             $image_attr = getimagesize($file);
  143.             
  144.             // figure out the longest side
  145.             
  146.             if($image_attr[0] > $image_attr[1]) {
  147.                 $image_width = $image_attr[0];
  148.                 $image_height = $image_attr[1];
  149.                 $image_new_width = $max_side;
  150.                 
  151.                 $image_ratio = $image_width/$image_new_width;
  152.                 $image_new_height = $image_height/$image_ratio;
  153.                 //width is > height
  154.             } else {
  155.                 $image_width = $image_attr[0];
  156.                 $image_height = $image_attr[1];
  157.                 $image_new_height = $max_side;
  158.                 
  159.                 $image_ratio = $image_height/$image_new_height;
  160.                 $image_new_width = $image_width/$image_ratio;
  161.                 //height > width
  162.             }
  163.             
  164.             $thumbnail = imagecreatetruecolor($image_new_width, $image_new_height);
  165.             @imagecopyresized($thumbnail, $image, 0, 0, 0, 0, $image_new_width, $image_new_height, $image_attr[0], $image_attr[1]);
  166.             
  167.             // move the thumbnail to it's final destination
  168.             
  169.             $path = explode('/', $file);
  170.             $thumbpath = substr($file, 0, strrpos($file, '/')) . '/thumb-' . $path[count($path)-1];
  171.             
  172.             if($type[2] == 1) {
  173.                 if(!imagegif($thumbnail, $thumbpath)) {
  174.                     $error = __("Thumbnail path invalid");
  175.                 }
  176.             } elseif($type[2] == 2) {
  177.                 if(!imagejpeg($thumbnail, $thumbpath)) {
  178.                     $error = __("Thumbnail path invalid");
  179.                 }
  180.             } elseif($type[2] == 3) {
  181.                 if(!imagepng($thumbnail, $thumbpath)) {
  182.                     $error = __("Thumbnail path invalid");
  183.                 }
  184.             }
  185.             
  186.         }
  187.     }
  188.     
  189.     if(!empty($error))
  190.     {
  191.         return $error;
  192.     }
  193.     else
  194.     {
  195.         return 1;
  196.     }
  197. }
  198.  
  199. // Some postmeta stuff
  200. function has_meta($postid) {
  201.     global $wpdb, $tablepostmeta;
  202.  
  203.     return $wpdb->get_results("
  204.         SELECT meta_key, meta_value, meta_id, post_id
  205.         FROM $tablepostmeta
  206.         WHERE post_id = '$postid'
  207.         ORDER BY meta_key,meta_id",ARRAY_A);
  208.  
  209. }
  210.  
  211. function list_meta($meta) {
  212.     global $post_ID;    
  213.     // Exit if no meta
  214.     if (!$meta) return;    
  215. ?>
  216. <table id='meta-list' cellpadding="3">
  217.     <tr>
  218.         <th><?php _e('Key') ?></th>
  219.         <th><?php _e('Value') ?></th>
  220.         <th colspan='2'><?php _e('Action') ?></th>
  221.     </tr>
  222. <?php
  223.         
  224.     foreach ($meta as $entry) {
  225.         $style = ('class="alternate"' == $style) ? '' : 'class="alternate"';
  226.         echo "
  227.     <tr $style>
  228.         <td valign='top'><input name='meta[{$entry['meta_id']}][key]' tabindex='6' type='text' size='20' value='{$entry['meta_key']}' /></td>
  229.         <td><textarea name='meta[{$entry['meta_id']}][value]' tabindex='6' rows='2' cols='30'>{$entry['meta_value']}</textarea></td>
  230.         <td align='center' width='10%'><input name='updatemeta' type='submit' class='updatemeta' tabindex='6' value='" . __('Update') ."' /></td>
  231.         <td align='center' width='10%'><input name='deletemeta[{$entry['meta_id']}]' type='submit' class='deletemeta' tabindex='6' value='" . __('Delete') ."' /></td>
  232.     </tr>
  233. ";
  234.     }
  235.     echo "
  236.     </table>
  237. ";
  238. }
  239.  
  240. // Get a list of previously defined keys
  241. function get_meta_keys() {
  242.     global $wpdb, $tablepostmeta;
  243.     
  244.     $keys = $wpdb->get_col("
  245.         SELECT meta_key
  246.         FROM $tablepostmeta
  247.         GROUP BY meta_key
  248.         ORDER BY meta_key");
  249.     
  250.     return $keys;
  251. }
  252.  
  253. function meta_form() {
  254.     global $wpdb, $tablepostmeta;
  255.     $keys = $wpdb->get_col("
  256.         SELECT meta_key
  257.         FROM $tablepostmeta
  258.         GROUP BY meta_key
  259.         ORDER BY meta_id DESC
  260.         LIMIT 10");
  261. ?>
  262. <h3><?php _e('Add a new custom field to this post:') ?></h3>
  263. <table cellspacing="3" cellpadding="3">
  264.     <tr>
  265. <th colspan="2"><?php _e('Key') ?></th>
  266. <th><?php _e('Value') ?></th>
  267. </tr>
  268.     <tr valign="top">
  269.         <td align="right" width="18%">
  270. <?php if ($keys) : ?>
  271. <select id="metakeyselect" name="metakeyselect" tabindex="7">
  272. <option value="#NONE#">- Select -</option>
  273. <?php
  274.     foreach($keys as $key) {
  275.         echo "\n\t<option value='$key'>$key</option>";
  276.     }
  277. ?>
  278. </select> or 
  279. <?php endif; ?>
  280. </td>
  281. <td><input type="text" id="metakeyinput" name="metakeyinput" tabindex="7" /></td>
  282.         <td><textarea id="metavalue" name="metavalue" rows="3" cols="25" tabindex="7"></textarea></td>
  283.     </tr>
  284.  
  285. </table>
  286. <p class="submit"><input type="submit" name="updatemeta" tabindex="7" value="<?php _e('Add Custom Field »') ?>"></p>
  287. <?php
  288. }
  289.  
  290. function add_meta($post_ID) {
  291.     global $wpdb, $tablepostmeta;
  292.     
  293.     $metakeyselect = $wpdb->escape( stripslashes( trim($_POST['metakeyselect']) ) );
  294.     $metakeyinput  = $wpdb->escape( stripslashes( trim($_POST['metakeyinput']) ) );
  295.     $metavalue     = $wpdb->escape( stripslashes( trim($_POST['metavalue']) ) );
  296.  
  297.     if (!empty($metavalue) && ((('#NONE#' != $metakeyselect) && !empty($metakeyselect)) || !empty($metakeyinput))) {
  298.         // We have a key/value pair. If both the select and the 
  299.         // input for the key have data, the input takes precedence:
  300.  
  301.         if ('#NONE#' != $metakeyselect)
  302.             $metakey = $metakeyselect;
  303.                 
  304.         if ($metakeyinput)
  305.             $metakey = $metakeyinput; // default
  306.  
  307.         $result = $wpdb->query("
  308.                 INSERT INTO $tablepostmeta 
  309.                 (post_id,meta_key,meta_value) 
  310.                 VALUES ('$post_ID','$metakey','$metavalue')
  311.             ");
  312.     }
  313. } // add_meta
  314.  
  315. function delete_meta($mid) {
  316.     global $wpdb, $tablepostmeta;
  317.  
  318.     $result = $wpdb->query("DELETE FROM $tablepostmeta WHERE meta_id = '$mid'");
  319. }
  320.  
  321. function update_meta($mid, $mkey, $mvalue) {
  322.     global $wpdb, $tablepostmeta;
  323.  
  324.     return $wpdb->query("UPDATE $tablepostmeta SET meta_key = '$mkey', meta_value = '$mvalue' WHERE meta_id = '$mid'");
  325. }
  326.  
  327. function touch_time($edit = 1) {
  328.     global $month, $postdata;
  329.     // echo $postdata['Date'];
  330.     if ('draft' == $postdata->post_status) {
  331.         $checked = 'checked="checked" ';
  332.         $edit = false;
  333.     } else {
  334.         $checked = ' ';
  335.     }
  336.  
  337.     echo '<p><input type="checkbox" class="checkbox" name="edit_date" value="1" id="timestamp" '.$checked.'/> <label for="timestamp">' . __('Edit timestamp') . '</label> <a href="http://wordpress.org/docs/reference/post/#edit_timestamp" title="' . __('Help on changing the timestamp') . '">?</a><br />';
  338.     
  339.     $time_adj = time() + (get_settings('gmt_offset') * 3600);
  340.     $post_date = $postdata->post_date;
  341.     $jj = ($edit) ? mysql2date('d', $post_date) : gmdate('d', $time_adj);
  342.     $mm = ($edit) ? mysql2date('m', $post_date) : gmdate('m', $time_adj);
  343.     $aa = ($edit) ? mysql2date('Y', $post_date) : gmdate('Y', $time_adj);
  344.     $hh = ($edit) ? mysql2date('H', $post_date) : gmdate('H', $time_adj);
  345.     $mn = ($edit) ? mysql2date('i', $post_date) : gmdate('i', $time_adj);
  346.     $ss = ($edit) ? mysql2date('s', $post_date) : gmdate('s', $time_adj);
  347.  
  348.     echo '<input type="text" name="jj" value="'.$jj.'" size="2" maxlength="2" />'."\n";
  349.     echo "<select name=\"mm\">\n";
  350.     for ($i=1; $i < 13; $i=$i+1) {
  351.         echo "\t\t\t<option value=\"$i\"";
  352.         if ($i == $mm)
  353.         echo " selected='selected'";
  354.         if ($i < 10) {
  355.             $ii = "0".$i;
  356.         } else {
  357.             $ii = "$i";
  358.         }
  359.         echo ">".$month["$ii"]."</option>\n";
  360.     } ?>
  361. </select>
  362. <input type="text" name="aa" value="<?php echo $aa ?>" size="4" maxlength="5" /> @ 
  363. <input type="text" name="hh" value="<?php echo $hh ?>" size="2" maxlength="2" /> : 
  364. <input type="text" name="mn" value="<?php echo $mn ?>" size="2" maxlength="2" /> : 
  365. <input type="text" name="ss" value="<?php echo $ss ?>" size="2" maxlength="2" /> </p>
  366.     <?php
  367. }
  368.  
  369. function check_admin_referer() {
  370.   $adminurl = strtolower(get_settings('siteurl')).'/wp-admin';
  371.   $referer = strtolower($_SERVER['HTTP_REFERER']);
  372.   if ( !strstr($referer, $adminurl) ) {
  373.     die('Sorry, you need to enable sending referrers, for this feature to work.');
  374.   }
  375. }
  376.  
  377. ?>